Skip to content

Auto-set allow_none when an Enum member has value None#2992

Open
youdie006 wants to merge 1 commit into
marshmallow-code:devfrom
youdie006:feat/enum-none-allow-none
Open

Auto-set allow_none when an Enum member has value None#2992
youdie006 wants to merge 1 commit into
marshmallow-code:devfrom
youdie006:feat/enum-none-allow-none

Conversation

@youdie006

@youdie006 youdie006 commented Jul 6, 2026

Copy link
Copy Markdown

Closes #2985.

fields.Enum now defaults allow_none=True when the passed Enum has a member whose value is None, mirroring fields.Constant(None) (as suggested on the issue). An explicit allow_none=False is preserved, so loading None then raises a ValidationError.

Added TestEnumAllowNone covering by-name and by-value, the explicit-allow_none=False error case, and regression guards (an enum without a None member stays allow_none=False; an explicit allow_none=True is honored).


AI disclosure: this PR was drafted with Claude Code (AI-assisted). The change was verified locally — the added TestEnumAllowNone cases fail without the fix and pass with it, and the full test suite (1186 tests) passes.

Mirror fields.Constant(None): fields.Enum now defaults allow_none to True
when the passed Enum contains a None-valued member, while preserving an
explicit allow_none=False (which then errors on None load).
Comment thread src/marshmallow/fields.py
@deckar01

deckar01 commented Jul 6, 2026

Copy link
Copy Markdown
Member

Drafted-by: Claude Code (Opus 4.8) (no human review before posting)

apache/airflow#69431

Any reason no AI disclosure was added to this PR?

@youdie006

Copy link
Copy Markdown
Author

Fair question, and apologies for the inconsistency. Yes — like the linked PR, this was drafted with Claude Code; I've added the same AI disclosure to the description here. The change is verified locally (the added TestEnumAllowNone cases fail without the fix and pass with it, and the full suite passes), but you're right that the disclosure should have been there from the start.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Question: Should fields.Enum() accept None when an enum member has value None?

2 participants